package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.entity.SysCourseTime;
import com.markerhub.entity.SysTime;
import com.markerhub.mapper.SysCourseTimeMapper;
import com.markerhub.service.SysCourseTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.markerhub.service.SysTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-03-31
 */
@Service
public class SysCourseTimeServiceImpl extends ServiceImpl<SysCourseTimeMapper, SysCourseTime> implements SysCourseTimeService {

    @Autowired
    SysTimeService sysTimeService;

    @Override
    public List<SysCourseTime> listSysCourseTimeByCourseId(Long CourseId) {
        List<SysCourseTime> sysCourseTime = this.list(new QueryWrapper<SysCourseTime>().eq("course_id",CourseId));
        return sysCourseTime;
    }

    @Override
    public List<SysCourseTime> getByCourse(Long id) {
        return this.list(new QueryWrapper<SysCourseTime>().inSql("course_id", String.valueOf(id)));
    }

    @Override
    public List<Long> listTimeByCollegeId(Long id) {
         List<SysCourseTime> time =this.list(new QueryWrapper<SysCourseTime>().inSql("course_id", String.valueOf(id)));
         Integer size=time.size();
        List<Long> times=new ArrayList<>();
         for (int i=0;i<size;i++){
             times.add(time.get(i).getTimeId());
         }
        return times;
    }

    @Override
    public Map<Long, String> generateTimeString(List <SysCourseTime> s) {
        Map<Long,String>time=new TreeMap<Long,String>();
        for(SysCourseTime t:s){
            Integer weekday= sysTimeService.getOne(new QueryWrapper<SysTime>().eq("id",t.getTimeId())).getWeekday();
            Integer timespan = sysTimeService.getOne(new QueryWrapper<SysTime>().eq("id",t.getTimeId())).getTimespan();
            StringBuilder stringBuilder= new StringBuilder("");
            stringBuilder.append("周").append(weekday).append("第").append(timespan).append("节");
            time.put(t.getTimeId(),stringBuilder.toString());
        }
        return time;
    }

    @Override
    public Boolean saveTime(Long CourseId,Map<Long,String> time){
        if(time==null)return false;
        Set<Long> timeId = time.keySet();
        for (Long i:timeId){
            SysCourseTime sysCourseTime = new SysCourseTime();
            sysCourseTime.setCourseId(CourseId);
            sysCourseTime.setTimeId(i);
            if(!this.save(sysCourseTime))return false;
        }
        return true;
    }

}
